package com.template.app.common.service.parser;

import com.template.app.common.enumerate.MaterialFileTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Component
public class MaterialParserFactory {
	
	@Autowired
	private ImgParser imgParser;
	
	@Autowired
	private PdfParser pdfParder;
	
	private Map<String, MaterialParser> parserMap = new HashMap<String, MaterialParser>();
	
	@PostConstruct
	public void init(){
		parserMap.put(MaterialFileTypeEnum.jpg.toString(), imgParser);//map的键值对为<jpg,img图像对象>
		parserMap.put(MaterialFileTypeEnum.gif.toString(), imgParser);
		parserMap.put(MaterialFileTypeEnum.png.toString(), imgParser);
		parserMap.put(MaterialFileTypeEnum.pdf.toString(), pdfParder);
	}
	
	public  MaterialParser getParserByType(String fileType) throws Exception{
		if(parserMap.containsKey(fileType)){
			return parserMap.get(fileType);
		}else{
			throw new Exception("不支持的文件类型.");
		}
	}


}
